<html>
<head>
<title>Cellular Automata</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="ca.js"></script> 
<style>

body {margin:0;padding:0}

#ca {
	top: 0;
	left: 0;
	position: fixed;
	cursor: pointer;
	z-index:1;
}

.copy {
	top: 0;
	left: 0;
	font-family: monospace;
	font-weight: bolder;
	position: fixed;
	color: #888;
	opacity: .5;
	z-index:10;
	font-size: 18px;
	margin: 4px 0px 0px 4px;
	text-decoration: none;
}

#size {
	top: 0;
	left: 0;
	position: fixed;
	z-index:2;
}

</style>
<script>

var Rule = akinao.ca.Rule;
var CA = akinao.ca.CellAutomata;
var Color = akinao.ca.Color;

var rules = [];
(function() {
	var r = {
		"Banners": "3457/2367,5",
		"BelZhab": "23/23,8",
		"BelZhab Sediment": "23/145678,8",
		"Bloomerang": "34678/234,24",
		"Bombers": "24/345,25",
		"Brain 6": "246/6,3",
		"Brain's Brain": "2/,3",
		"Burst": "3468/0235678,9",
		"Burst II": "3468/235678,9",
		"Caterpillars": "37/124567,4",
		"Chenille": "24567/05678,6",
		"Circuit Genesis": "1234/2345,8",
		"Cooties": "2/23,8",
		"Ebb&Flow": "36/012478,18",
		"Ebb&Flow II": "37/0123468,18",
		"Faders": "2/2,25",
		"Fireworks": "13/2,21",
		"Flaming Starbows": "23/347,8",
		"Frogs": "34/12,3",
		"Frozen spirals": "23/356,6",
		"Glisserati": "245678/035678,7",
		"Glissergy": "245678/035678,5",
		"Lava": "45678/12345,8",
		"Lines": "458/012345,3",
		"LivingOn TheEdge": "3/345,6",
		"Meteor Guns": "3/01245678,8",
		"Nova": "2478/45678,25",
		"OrthoGo": "2/3,4",
		"Prairie on fire": "34/345,6",
		"RainZha": "23/2,8",
		"Rake": "2678/3467,6",
		"SediMental": "25678/45678,4",
		"Snake": "25/03467,6",
		"SoftFreeze": "38/13458,6",
		"Spirals": "234/2,5",
		"Star Wars": "2/345,4",
		"Sticks": "2/3456,6",
		"Swirl": "34/23,8",
		"ThrillGrill": "34/1234,48",
		"Transers": "26/345,5",
		"TransersII": "26/0345,6",
		"Wanderers": "34678/345,5",
		"Worms": "25/3467,6",
		"Xtasy": "2356/1456,16",
		"Transit": "2678/45678,6",
		"Mineral": "3456/45678,16",
		"Frost crystals": "25678/34678,40",
		"2x2": "36/125",
		"34 Life": "34/34",
		"Amoeba": "357/1358",
		"Assimilation": "345/4567",
		"Coagulations": "378/235678",
		"Conway's Life": "3/23",
		"Coral": "3/45678",
		"Day & Night": "3678/34678",
		"Diamoeba": "35678/5678",
		"Flakes": "3/012345678",
		"Gnarl": "1/1",
		"HighLife": "36/23",
		"InverseLife": "0123478/23678",
		"Long life": "345/5",
		"Maze": "3/12345",
		"Mazectric": "3/1234",
		"Move": "368/245",
		"Pseudo life": "357/238",
		"Replicator": "1357/1357",
		"Seeds (2)": "2/",
		"Seriettes": "234/",
		"Stains": "3678/235678",
		"WalledCities": "45678/2345",
	};
	for (var n in r) {
		rules[n] = Rule.generations(n, r[n]);
	}
})();

var colors = {
		panton: new Color.RepeatedColor([
			"black",
			"orange",
			"tomato",
			"red",
			"crimson",
			"purple",
			"indigo",
			"navy",
			"blue",
			"darkgreen",
		]),
		ringLamp: new Color.RepeatedColor([
			"black",
			"floralwhite",
			"red",
			"coral",
			"maroon",
			"gold",
			"crimson",
		]),
		monotone: new Color.RepeatedColor([
			"whitesmoke",
			"gray",
			"gainsboro",
			"darkgray",
			"lightgray",
			"dimgray",
		]),
		gradation: new Color.GradationalColor(
			0xf0f0f0,
			0x606060,
			0xffffff
		),
		eightsum: new Color.EightFourSumColor([
			"whitesmoke", // background
			"moccasin", // 0a
			"skyblue", // 1a
			"deepskyblue", // 1b
			"blue", // 2a
			"mediumblue", // 2b
			"dodgerblue", // 2c
			"green", // 3a
			"darkgreen", // 3b
			"forestgreen", // 3c
			"limegreen", // 3d
			"coral", // 4a
			"orangered", // 4b
			"red", // 4c
			"crimson", // 4d
			"maroon", // 4e
			"yellow", // 5b
			"gold", // 5c
			"orange", // 5d
			"darkorange", // 5e
			"purple", // 6c
			"indigo", // 6d
			"magenta", // 6e
			"darkcyan", // 7d
			"darkslategray", // 7e
			"gray", // 8e
		]),
		eightsumMono: new Color.EightFourSumColor([
   			"#ffffff", // background
   			"#707070", // 0a
   			"#808080", // 1a
   			"#888888", // 1b
   			"#909090", // 2a
   			"#969696", // 2b
   			"#9b9b9b", // 2c
   			"#a0a0a0", // 3a
   			"#a4a4a4", // 3b
   			"#a8a8a8", // 3c
   			"#acacac", // 3d
   			"#b0b0b0", // 4a
   			"#b3b3b3", // 4b
   			"#b6b6b6", // 4c
   			"#b9b9b9", // 4d
   			"#bbbbbb", // 4e
   			"#c0c0c0", // 5b
   			"#c4c4c4", // 5c
   			"#c8c8c8", // 5d
   			"#cccccc", // 5e
   			"#d0d0d0", // 6c
   			"#d6d6d6", // 6d
   			"#dbdbdb", // 6e
   			"#e0e0e0", // 7d
   			"#e8e8e8", // 7e
   			"#f0f0f0", // 8e
   		]),
	};

var ca = null;

function next() {
	ca.next();
	setTimeout(next, 10);
}

$(window).resize(function() {
	resize();
});

$(window).load(function () {
	var params = getQueryParams();
	if (params) {
		if (params.r && rules[params.r]) {
			rule = rules[params.r];
		}
		if (params.c && colors[params.c]) {
			color = colors[params.c];
		}
		if (params.i) {
			random = params.i;
		}
	}
	resize();
	next();
	$('#ca').click(function() {
		resize();
		});
	});

var rule = rules["Star Wars"];
var color = colors.eightsum;
var random = 0.40;

var MAX = 160;
function resize() {
	var c = $('#ca');
	var w = $(window).width();
	var h = $(window).height();
	c.width(w).height(h);
	var u = universeSize(w, h);
	ca = new CA({
		element: document.getElementById("ca"),
		universe: {x: u.x, y: u.y, closed: true},
		rule: rule, 
		color: color,
		pix: u.pix,
	});
	ca.next(Rule.random(random));
}

function universeSize(w, h) {
	var s = w > h ? w : h;
	var p = div(s, MAX);
	var x = div(w, p);
	var y = div(h, p);
	return {pix:p, x:x, y:y};
}

function div(n, d) {
	var r = parseInt(n / d);
	if (0 < n % d) r++;
	return r;
}

function getQueryParams() {
	var qs = location.search;
	if (qs) {
		var qsa=qs.substring(1).split('&');
 		var params={};
		for (var i = 0; i < qsa.length; i++) {
			var pair=qsa[i].split('=');
			if (pair[0]) {
				params[pair[0]]=decodeURIComponent(pair[1]);
			}
		}
		return params;
	}
	return null;
}
</script>
</head>
<body>
<a class="copy" href="http://www.akinao.com">akinao.com</a>
<canvas id="ca"></canvas>
</body>
</html>
